﻿using EyFramework.ORM;
using Ncov2019.Entity;
using Ncov2019.Entity.Dto;
using SqlSugar;
using System.Collections.Generic;

namespace Ncov2019.Core.Dao
{
    public class NcovBarConfirmDao : BaseDaoHis<NcovBarConfirm>, INcovBarConfirmDao
    {
        public bool DeleteById(long confirmSeq)
        {
            return Db.Ado.ExecuteCommand("DELETE FROM NCOV_BAR_CONFIRM WHERE CONFIRM_SEQ=" + confirmSeq) >= 0;
        }

        public LisReportInfo GetLisReport(string bingrenid)
        {
            string sql = "SELECT TIAOMAHAO,YANGBENHAO,BINGRENID,XINGMING,DECODE(XINGBIE,'1','男','2','女') XINGBIE,NIANLING||DECODE(NIANLINGDW,'1','岁') NIANLING,ZHENDUANMC,KAIDANKSMC,KAIDANYSXM,TO_CHAR(CAIJISJ,'YYYY-MM-DD HH24:MI') CAIJISJ," +
                "TO_CHAR(JIESHOUSJ,'YYYY-MM-DD HH24:MI') JIESHOUSJ,TO_CHAR(SHENHESJ,'YYYY-MM-DD HH24:MI') SHENHESJ,YANGBENLXMC,JIESHOURXM,SHENHERXM FROM lims6xs.lcb_yangbenxx where BINGRENID=:BINGRENID AND ROWNUM=1";
            return Db.Ado.SqlQuerySingle<LisReportInfo>(sql,
               new
               {
                   BINGRENID = bingrenid
               });
        }

        public List<NcovBarConfirm> GetNcovBarConfirm(NcovBarConfirm ncovBarConfirm)
        {
            return Db.Queryable<NcovBarConfirm>().Where(p => p.TIAOMAHAO == ncovBarConfirm.TIAOMAHAO).ToList();
        }

        public NcovBarInfoDto GetNcovBarInfo(string barCode)
        {
            return Db.Queryable<NcovBarInfoDto>().Where(p => p.TIAOMAHAO == barCode).First();
        }

        public List<NcovInfo> QueryNcovInfo(NcovInfoParam param)
        {
            var sql = @"SELECT * FROM V_NCOV_INFO WHERE 1= 1";
            if (!string.IsNullOrEmpty(param.CHT_ID))
            {
                sql = sql + " and BINGRENID = :BINGRENID";
            }

            if (!string.IsNullOrEmpty(param.PAT_NM))
            {
                sql = sql + " and XINGMING = :XINGMING";
            }
            string sDateWhere = "";
            //0采集日期
            //1确认日期
            //2接收日期
            //3报告日期
            if (param.QUERYTYPE == 1)
            {
                sDateWhere = "QUERENSJ";
            }
            else if (param.QUERYTYPE == 2)
            {
                sDateWhere = "JIESHOUSJ";
            }
            else if (param.QUERYTYPE == 3)
            {
                sDateWhere = "SHENHESJ";
            }
            else
            {
                sDateWhere = "CAIJISJ";
            }
            if (!string.IsNullOrEmpty(param.START_DT))
            {
                sql = sql + " and " + sDateWhere + " >= TO_DATE(:START_DT,'YYYY-MM-DD HH24:MI:SS')";
            }

            if (!string.IsNullOrEmpty(param.END_DT))
            {
                sql = sql + " and " + sDateWhere + " <= TO_DATE(:END_DT,'YYYY-MM-DD HH24:MI:SS')";
            }
            sql = sql + "order by querensj";
            return Db.Ado.SqlQuery<NcovInfo>(sql,
                new
                {
                    BINGRENID = param.CHT_ID,
                    XINGMING = param.PAT_NM,
                    START_DT = param.START_DT + " 00:00:00",
                    END_DT = param.END_DT + " 23:59:59"
                });
        }

        public List<NcovBarConfirm> queryTodayData(NcovBarConfirmParam ncovBarConfirmParam)
        {
            return Db.Queryable<NcovBarConfirm>().Where(p => p.CAIJIRQ == ncovBarConfirmParam.CAIJIRQ && p.CAIJIR == ncovBarConfirmParam.CAIJIR).ToList();
        }

        /// <summary>
        /// 调用存储过程完成检验基本信息的修改
        /// </summary>
        /// <param name="lisPatInfo"></param>
        /// <returns></returns>
        public bool UpdateLisPatInfo(LisPatInfoDto lisPatInfo)
        {
            var PRM_BINGRENID = new SugarParameter(":PRM_BINGRENID", lisPatInfo.BINGRENID);
            var PRM_XINGMING = new SugarParameter(":PRM_XINGMING", lisPatInfo.XINGMING);
            var PRM_XINGBIE = new SugarParameter(":PRM_XINGBIE", lisPatInfo.XINGBIE);
            var PRM_CHUSHENGRQ = new SugarParameter(":PRM_CHUSHENGRQ", lisPatInfo.CHUSHENGRQ);
            var PRM_SHENFENZH = new SugarParameter(":PRM_SHENFENZH", lisPatInfo.SHENFENZH);
            var PRM_NIANLING = new SugarParameter(":PRM_NIANLING", lisPatInfo.NIANLING);
            var PRM_NIANLINGDW = new SugarParameter(":PRM_NIANLINGDW", lisPatInfo.NIANLINGDW);
            var PRM_APPCODE = new SugarParameter(":PRM_APPCODE", null, true);
            var reslt = Db.Ado.UseStoredProcedure().GetDataTable("LIMS6XS.PRO_UPD_BRXX",
                PRM_BINGRENID,
                PRM_XINGMING,
                PRM_XINGBIE,
                PRM_CHUSHENGRQ,
                PRM_SHENFENZH,
                PRM_APPCODE,
                PRM_NIANLING,
                PRM_NIANLINGDW);
            return "0" == PRM_APPCODE.Value.ToString();
        }
    }
}